/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/11/25 19:58
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */


(function () {

    "use strict";

    var api = {
        getTicket: "/promoter/ticket/getLists",
        changeStatus: "/promoter/ticket/changeStatus"
    };

    sm.controller("admin-promoter-ticket-index-controller", ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.page = {
            total: 0,
            nowPage: 1,
            nextPage: 1
        };

        $scope.tickets = [];

        $scope.getLists = function () {
            $http.get(api.getTicket, {
                params: {
                    page: $scope.page.nextPage
                }
            }).success(function (res) {

                if (res.state) {
                    $scope.page.total = res.data.total;
                    $scope.tickets = res.data.data;
                }


            });
        };

        $scope.getLists();

        /**
         * 修改状态
         * @param index
         * @returns {boolean}
         */
        $scope.changeStatus = function (index) {
            if (!$scope.tickets[index].id) {
                return false;
            }

            var nowStatus = $scope.tickets[index]['status'];
            var status = nowStatus == 1 ? 0 : 1;

            Modal.alert({
                title: "您确定修改吗?",
                content: "状态修改可能造成不可用,请谨慎操作!",
                buttons: [{
                    name: "取消",
                    type: "cancel"
                }, {
                    name: "确定修改",
                    event: function () {
                        doChangeStatus(index, status);
                    }
                }]
            });
        };

        /**
         * 删除
         * @returns {boolean}
         */
        $scope.deleteTicket = function (index) {
            if (!$scope.tickets[index].id) {
                return false;
            }

            var status = -1;

            Modal.error({
                title: "您确定删除吗?",
                content: "删除可能对用户产生影响,请谨慎操作!",
                buttons: [{
                    name: "取消",
                    type: "cancel"
                }, {
                    name: "确定删除",
                    event: function () {
                        doChangeStatus(index, status);
                    }
                }]
            });
        };

        function doChangeStatus(index, status) {
            $http.post(api.changeStatus, {
                id: $scope.tickets[index]['id'],
                status: status
            }).success(function (res) {
                if (res.state) {
                    if (status == -1) {
                        $scope.tickets.splice(index, 1);
                    } else {
                        $scope.tickets[index]['status'] = status;
                    }
                } else {
                    Modal.error({title: "修改失败"});
                }
            });
        }


    }]);
})();
